Function.RegisterNamespace("System.IO.FileStrategy.V8");

System.IO.FileStrategy.V8.Windows=function(){
    // Private Members
    var _bufferSize=7900;
    var _unicodeRegex=/[^\u0000-\u00ff]/;

    // IFileStrategy Members
    this.DeleteFile=function(path){
        if(this.Exists(path)){
            return System.Environment.Execute("del",["/F",String.Format("\"{0}\"",System.IO.Path.Normalize(escapePath(path)))]);
        }
        return false;
    };
    
    this.Exists=function(path){
        if(System.Environment.Execute("cmd", ["/C",String.Format("IF NOT EXIST \"{0}\" (ECHO 1)",escapePath(path))])==1)return false;
        return System.Environment.Execute("cmd", ["/C",String.Format("2>NUL PUSHD \"{0}\" && (POPD&ECHO 0) || (echo 1)",escapePath(path))])==1;
    };

    this.GetFile=function(path){
        if(this.Exists(path)){
            return read(System.IO.Path.Normalize(path));
        }
        return null;
    };
        
    this.SaveFile=function(path,text){
// Unicode BOM attempt
//        if(/[^\u0000-\u00ff]/.test(text)){
//            try{
//                System.Environment.Execute("cmd", ["/D","/A","/C",String.Format("CHCP 1252 >NUL & (SET/P={0}{1})<NUL >\"{2}\" 2>NUL",String.fromCharCode(0xFF),String.fromCharCode(0xFE),path)],true);
//            }catch(e){}
//        }
        this.DeleteFile(path);
        if(text==undefined||String.IsEmpty(text))return System.Environment.Execute("cmd", ["/C",String.Format("ECHO. 2>\"{0}\">NUL",escapePath(path))],true);
        var chunks=Math.ceil(text.length/_bufferSize);
        for(var i=0;i<chunks;i++){
            var index=i*_bufferSize;
            var chunk=text.slice(index,index+_bufferSize);
            var lines=chunk.split(System.Environment.GetNewLine());
            for(var j=0;j<lines.length;j++){
                if(j)System.Environment.Execute("cmd", ["/C",String.Format("ECHO.>>\"{0}\"",escapePath(path))],true);
                try{
                    if(_unicodeRegex.test(lines[j])){
                        System.Environment.Execute("cmd", ["/U /C",String.Format("<NUL SET /P =\"{0}\" >> \"{1}\"",escapeLine(lines[j]),escapePath(path))],true);
                    }else{
                        System.Environment.Execute("cmd", ["/C",String.Format("<NUL SET /P =\"{0}\" >> \"{1}\"",escapeLine(lines[j]),escapePath(path))],true);
                    }
                }catch(e){}
            }
        }
    };

    // IStrategySpecification Members
    this.IsSatisfiedBy=function(candidate){
        if(typeof(os)!='undefined' && typeof(os.system)!='undefined'){
            try{
                os.system("ver");
                System.IO.Path.DirectorySeparator="\\";
                return true;
            }catch(e){}
        }
        return false;
    };

    // Private Methods
    function escapeLine(line){
        return (line||'').replace(/\^/g,'^^^').replace(/([|<>])/g,'^$1').replace(/"/g,"\"\"")
    }
    
    function escapePath(path){
        return (path||'').replace(/\^/g,'^^').replace(/&/g,'^&');
    }

};

System.IO.FileStrategy.V8.Windows.Implement(System.IO.FileStrategy.IFileStrategy,'System.IO.FileStrategy.V8.Windows');
System.IO.FileStrategy.V8.Windows.Implement(System.Script.Strategy.IStrategySpecification,'System.IO.FileStrategy.V8.Windows');

System.IO.File.Strategies.Add(System.IO.FileStrategy.V8.Windows);